package DataModel
{
	import flash.errors.IllegalOperationError;
	
	public class ReferenceData
	{
		static public var ministries:Object = new Object();
		static public var departments:Object = new Object();
		static public var provinces:Object = new Object();
		static public var districts:Object = new Object();
		static public var caseTypes:Array = new Array();
		static public var payStations:Object = new Object();
		static public var countries:Object = new Object();
		static public var institutionsTypes:Object = new Object();
		static public var acadInstitutions:Object = new Object();
		static public var acadFields:Object = new Object();
		static public var qualifications:Object = new Object();
		static public var univInstitutions:Object = new Object();
		static public var univFields:Object = new Object();
		static public var univQuals:Object = new Object(); 
		static public var profInstitutions:Object = new Object();
		static public var profFields:Object = new Object();
		static public var profQuals:Object = new Object(); 
		static public var trainingInstitutions:Object = new Object();
		static public var trainingCourses:Object = new Object();
		
		static public var casesLoaded:Boolean = false;
			
		static private var mainFix:ReferenceData = new ReferenceData();
	
		public function casesLoadedChange():void
		{
			casesLoaded = true;
		}
		static public function get instance():ReferenceData
		{
			return mainFix;						
		}
		
		
		[Bindable] // Not really, but it eliminates a warning
		static public var maritalStatuses:Array = 
			[	"",
				"Unmarried",
				"Married",
				"Separated",
				"Divorced",
				"Widowed"
			];
		
		public function loadAcadInstitutions(xml:XMLList):void
		{
			acadInstitutions = new Object;
			var lst:XMLList = xml;
			for each (var acadInstitution:XML in lst)
			{
				acadInstitutions[acadInstitution.InstitutionName] = acadInstitution.InstitutionName;  
			}		
			
		}
		
		static public function loadMinistries(xml:XMLList):void
		{
			ministries = new Object;
			var lst:XMLList = xml.Ministry;
			for each (var ministry:XML in lst)
			{
				ministries[ministry.MinistryID.toString().toUpperCase()] = ministry.MinistryDesc.toString();  
			}			 
		}
		static public function loadCaseTypes(xml:XMLList):void
		{
			caseTypes = new Array;
			var lst:XMLList = xml.CaseType;
			for each (var caseTypesxml:XML in lst)
			{
				caseTypes[caseTypesxml.CaseTypeID.toString().toUpperCase()] = caseTypesxml.Name.toString();  
			}	
		}

		static public function loadDepartments(xml:XMLList):void
		{
			departments = new Object;
			var lst:XMLList = xml.Department;
			for each (var department:XML in lst)
			{
				departments[department.DepartmentCode.toString().toUpperCase()] = department.DepartmentDesc.toString();  
			}			 
		}

		static public function loadProvinces(xml:XMLList):void
		{
			provinces = new Object;
			var lst:XMLList = xml.Province;
			provinces[0] = "";
			for each (var province:XML in lst)
			{
				provinces[province.ProvinceCode.toString().toUpperCase()] = province.ProvinceName.toString();  
			}			 
		}

		static public function loadDistricts(xml:XMLList):void
		{
			districts = new Object;
			var lst:XMLList = xml.District;
			for each (var district:XML in lst)
			{
				var districtObj:District =
					new District(	district.DistrictCode,
									district.DistrictName,
									district.ProvinceCode);
				districts[districtObj.districtCode] = districtObj; 
			}
		}

		static public function loadPayStations(xml:XMLList):void
		{
			payStations = new Object;
			var lst:XMLList = xml.PayStation;
			for each (var payStation:XML in lst)
			{
				var stationObj:PayStation = 
					new PayStation(	payStation.MinistryID, 
									payStation.DeptCode,
									payStation.PayStationCode,
									payStation.PayStationDesc,
									payStation.RegionCode,
									payStation.ProvinceCode,
									payStation.DistrictCode);
				var strKey:String = stationObj.xmlKey;
				  
				payStations[strKey] = stationObj;  
			}			 
		}

		static public function loadCountries(xml:XMLList):void
		{
			countries = new Object;
			var lst:XMLList = xml.Country;
			for each (var country:XML in lst)
			{
				countries[country.ISO.toString().toUpperCase()] = country.CountryName.toString();  
			}
		}

		static public function loadInstitutionsTypes(xml:XMLList):void
		{
			institutionsTypes = new Object;
			var lst:XMLList = xml.InstitutionType;
			for each (var institutionType:XML in lst)
			{
				institutionsTypes[institutionType.ID.toUpperCase()] = institutionType.Description.toString();
			}
		}

		static public function loadAcadFields(xml:XMLList):void
		{
			acadFields = new Object;
			var lst:XMLList = xml.AcadField;
			for each (var acadField:XML in lst)
			{
				acadFields[acadField.ID.toUpperCase()] = acadField.Description.toString();
			}
		}

		static public function loadQualifications(xml:XMLList):void
		{
			qualifications = new Object;
			var lst:XMLList = xml.Qualification;
			for each (var qualification:XML in lst)
			{
				var qualificationObj:Qualification = 
					new Qualification(	qualification.ID,
										qualification.Description,
										qualification.FieldId);
				  
				qualifications[qualificationObj.id] = qualificationObj;
			}
		}

		static public function loadEditableList(xml:XMLList):Object
		{
			var listEdit:Object = new Object;
			var i:int = 0;
			for each (var xmlEdit:XML in xml)
			{
				listEdit[i] = xmlEdit.toString();
				i++;
			}
			return listEdit;
		}

	}
}